package com.zty.utils;

import com.zty.BO.FileChunkMeta;
import com.zty.errors.BusinessException;
import com.zty.errors.EnumClientException;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author ZTY
 * @Description 分片下载策略模式
 * @Date 18:16 2023-12-26
 **/

@Component
public class ChunkDownloaderStrategy {
    private final Map<String,ChunkDownloader> downloaderMap;

    public ChunkDownloaderStrategy(List<ChunkDownloader> downloaderList){
        this.downloaderMap = new HashMap<>();
        downloaderList.forEach(downloader->downloaderMap.put(downloader.schema(),downloader));
    }

    public byte[] download(FileChunkMeta fileChunkMeta){
        String schema = fileChunkMeta.getSchema();
        ChunkDownloader chunkDownloader = downloaderMap.get(schema);
        if (Objects.isNull(chunkDownloader)){
            throw new BusinessException(EnumClientException.SCHEMA_DOES_NOT_SUPPORT);
        }
        return chunkDownloader.download(fileChunkMeta);
    }
}
